import CameraCapture from "../../base/Script/components/CameraCapture";
import DrawBorad from "../../base/Script/components/DrawBorad";
import TouchPlus from "../../base/Script/components/TouchPlus";
import BaseScene from "../../base/Script/core/BaseScene";
import BundleManager from "../../base/Script/core/BundleManager";
import BundleUpdateModule from "../../base/Script/module/BundleUpdateModule";
import { FileUtils } from "../../base/Script/utils/FileUtil";
import ImageUtil from "../../base/Script/utils/ImageUtil";
import LogUtil from "../../base/Script/utils/LogUtil";

const { ccclass, property } = cc._decorator;

//射箭游戏

@ccclass
export default class Bundle1Scene extends BaseScene {

    @property(cc.Sprite)
    sprite: cc.Sprite = null;

    @property(CameraCapture)
    camereCapture: CameraCapture = null;
    @property(DrawBorad)
    drawBorad: DrawBorad = null;
    @property(TouchPlus)
    touchPlus: TouchPlus = null;


    private _data = null;
    private _texture = null;
    
    onLoad(): void {
        super.onLoad();
        let labVersion = this.node.getChildByName('labelVersion').getComponent(cc.Label);
        labVersion.string = BundleUpdateModule.ins().getBundleVersion('bundle1');
    }

    start() {
        this.touchPlus.addEvent(null, (e) => {
            let delta = e.getDelta();
            this.touchPlus.node.x += delta.x;
            this.touchPlus.node.y += delta.y;
        });

        this.onBtnCapture();
    }

    onBtnCapture(){
        let data = this.camereCapture.captureTexture();
        this._data = data;
        let texture = new cc.Texture2D()
        texture.initWithData(data, cc.Texture2D.PixelFormat.RGBA8888, this.drawBorad.node.width, this.drawBorad.node.height);        
        this.sprite.spriteFrame = new cc.SpriteFrame(texture);        
        this.sprite.spriteFrame.setFlipY(true);
        this.drawBorad.setData(data);
        this._texture = texture;
    }

    onBtnPen(){
        this.drawBorad.setPen();
    }

    onBtnReaser(){
        this.drawBorad.setReaser();
    }

    onBtnSaveScreenshot(){
        ImageUtil.onSaveTap(this.sprite,this._data);
    }

    onDestroy() {
        super.onDestroy();
        BundleManager.ins().releaseAll('bundle1');
    }
}